package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.StudySystemException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author tang
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends
        ServiceImpl<TeachplanMapper, Teachplan>
        implements TeachplanService {
    private final TeachplanMediaMapper teachplanMediaMapper;
    private final TeachplanMapper teachplanMapper;
    public TeachplanServiceImpl(TeachplanMapper teachplanMapper, TeachplanMediaMapper teachplanMediaMapper) {
        this.teachplanMapper = teachplanMapper;
        this.teachplanMediaMapper = teachplanMediaMapper;
    }

    @Override
    public List<TeachplanDto> selectTeachplanNodes(Long courseId) {
        return teachplanMapper.selectTeachplanNodes(courseId);
    }

    @Override
    public void updateTeachplan(SaveTeachplanDto saveTeachplanDto) {
        Teachplan teachplan = new Teachplan();
        BeanUtils.copyProperties(saveTeachplanDto, teachplan);
        if (saveTeachplanDto.getId() == null) {
            teachplan.setCreateDate(LocalDateTime.now());
            teachplan.setChangeDate(LocalDateTime.now());
            Integer count = teachplanMapper.selectCount(saveTeachplanDto.getCourseId(), saveTeachplanDto.getParentid());
            teachplan.setOrderby(count + 1);
            int insert = teachplanMapper.insert(teachplan);
            if (insert <= 0) {
                StudySystemException.cast("添加章节失败");
            }
        } else {
            int update = teachplanMapper.updateById(teachplan);
            if (update <= 0) {
                StudySystemException.cast("更新章节失败");
            }
        }
        saveTeachplanDto = null;
    }

    @Override
    public void deleteTeachplan(Long id) {
        if (id == null) {
            StudySystemException.cast("章节id不能为空");
        }
        int delete = teachplanMapper.deleteById(id);
        if (delete <= 0) {
            StudySystemException.cast("删除章节失败");
        }
    }

    @Override
    public void movedown(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (Objects.isNull(teachplan)) {
            StudySystemException.cast("查询的章节不存在");
        }
        teachplan.setOrderby(teachplan.getOrderby() + 1);
        int update = teachplanMapper.updateById(teachplan);
        if (update <= 0) {
            StudySystemException.cast("章节移动失败");
        }
    }

    @Override
    public void moveup(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (Objects.isNull(teachplan)) {
            StudySystemException.cast("查询的章节不存在");
        }
        teachplan.setOrderby(teachplan.getOrderby() - 1);
        int update = teachplanMapper.updateById(teachplan);
        if (update <= 0) {
            StudySystemException.cast("章节移动失败");
        }
    }

    @Override
    @Transactional
    public void bindMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Teachplan teachplan = teachplanMapper.selectById(bindTeachplanMediaDto.getTeachplanId());
        if (Objects.isNull(teachplan)) {
            log.error("课程计划不存在, teachplan value is null");
            return;
        }
        // 根据课程计划id删除其绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId,
                        bindTeachplanMediaDto.getTeachplanId()));

        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto, teachplanMedia);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
    }


}
